package com.cjm.algorithm.tree;

import com.cjm.algorithm.TreeNode;

import java.util.LinkedList;

public class Ti226 {
    public static void main(String[] args) {

    }

    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        LinkedList<TreeNode> ts = new LinkedList<>();
        ts.add(root);
        while (!ts.isEmpty()) {
            int size = ts.size();
            for (int i = 0; i < size; i++) {
                TreeNode pop = ts.pop();
                TreeNode left = pop.left;
                pop.left = pop.right;
                pop.right = left;

                if (pop.right != null) ts.add(pop.right);
                if (pop.left != null) ts.add(pop.left);
            }
        }
        return root;
    }
}
